package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import Communication.Client;
import Communication.MessageHandle;
import Controller.PatternController.NewPatternListener;
import Entity.Pattern;
import Entity.Product;
import DB.PatternQuery;
import Entity.Customer;
import Entity.Message;
import Entity.Cities;
import View.*;
//import View.NewProductToCatalogView;
/** This class controls and manages the process that allows to create new pattern*/
public class PatternController implements Serializable {
	private static final long serialVersionUID = 42L;
	
	/**  Required attributes */
	static PatternConfigView patternView;
	Client client;
	PatternQuery patternModel;
	static ArrayList<Pattern> patternList=new ArrayList<Pattern>();
	Pattern Ptr;
	
	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		if (msg.compareTo("new")==0)
			SystemController.displayMessage("New pattern successfully added");
		else if(msg.compareTo("product not exists")==0) 
			SystemController.displayErrorMessage("The product is not exists");
		else if(msg.compareTo("exist")==0)
			SystemController.displayErrorMessage("The pattern is allready exists");
	}
	
	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public PatternController (Client client, WorkerMainMenuController workerMainMenuController) {
		this.client = client;
		patternView = new PatternConfigView();
		patternView.PatternConfigListener(new NewPatternListener(workerMainMenuController));
		getCites("addPattern");
	}
	
	/**
	 * function that gets cities to show in combobox
	 * @param message
	 */
	private void getCites(String message) {
		MessageHandle msg = new MessageHandle(null, MessageHandle.GET_CITIES, false);
		msg.setMsg(message);
		try {
			client.sendToServer(msg);
		} catch (IOException e) {
			System.out.print("PatternController->GetCities");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	* Handle Actions
	 */
	class NewPatternListener implements ActionListener
	{
		WorkerMainMenuController workerMainMenuController;
		
		/**
		 * creats new pattern listener 
		 * @param workerMainMenuController
		 */
		public  NewPatternListener(WorkerMainMenuController workerMainMenuController){
			this.workerMainMenuController = workerMainMenuController;
			patternModel = new PatternQuery();
			patternView.showWindow();
		}

		public void actionPerformed(ActionEvent event) {
			
			
			if (event.getSource() == patternView.getConfigureButton())
			{      
				if (patternModel.isEmpty(patternView.getProductID())   
					||patternModel.isEmpty(patternView.getMaxAge())               
					||patternModel.isEmpty(patternView.getMinAge())
					//||patternModel.isEmpty(patternView.getCustomerCity())
					||patternModel.isEmpty(patternView.getCustomerImportance())
					||patternModel.isEmpty(patternView.getCustomerInteresting())
					||patternModel.isEmpty(patternView.getCustomerPermission())
					||patternModel.isEmpty(patternView.getMassageToCustomer())
					||patternModel.isEmpty(patternView.getPatternID())
					||patternModel.isEmpty(patternView.getPatternName()))
						SystemController.displayErrorMessage("One or more fields are Empty");
				
				else 
				{ 
					Pattern Ptr=new Pattern();
					Ptr.setPatternID(patternView.getPatternID().toString());
					Ptr.setPatternName(patternView.getPatternName());
					Ptr.setProductID(patternView.getProductID());
					Ptr.setMinAge(Integer.parseInt(patternView.getMinAge()));
					Ptr.setMaxAge(Integer.parseInt(patternView.getMaxAge()));
					Ptr.setCity(patternView.getCustomerCity());
					Ptr.setImportance(Integer.parseInt(patternView.getCustomerImportance()));
					Ptr.setInterest(Integer.parseInt(patternView.getCustomerInteresting()));
					Ptr.setPermission(Integer.parseInt(patternView.getCustomerPermission()));
					Ptr.setMessage(patternView.getMassageToCustomer());
					try {
						MessageHandle msg = new MessageHandle(Ptr, MessageHandle.PATTERN, false);
						client.sendToServer(msg);
						
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
					
					workerMainMenuController.hideWindow();
					
					
				}
			}
			else if (event.getSource() == patternView.getBackButton())
			{
				workerMainMenuController.showWindow();
				patternView.hideWindow();
				patternView.clearFields();
				//workerMainMenuController.hideWindow();
				
			}		
		}	
	}
	
	/**
	 * function that adds cities to the cities list to show in combobox
	 * @param cities
	 */
	public static void addCities(String [] cities){
		patternView.setCitys(cities);
	}
}
